package com.example.playaudiotest;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private int music_time = 0;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMediaPlayer();
        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!mediaPlayer.isPlaying())
                    mediaPlayer.start();

                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (mediaPlayer.isPlaying()){
                            music_time++;
                            ((SeekBar)findViewById(R.id.seekBar)).setProgress(music_time);
                        }
                        else
                            timer.cancel();
                    }
                },0,1000);
            }
        });
        findViewById(R.id.pause).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying())
                    mediaPlayer.pause();
            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.reset();
                    initMediaPlayer();
                    music_time = 0;
                    ((SeekBar)findViewById(R.id.seekBar)).setProgress(0);
                }
            }
        });

        ((SeekBar)findViewById(R.id.seekBar)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                ((TextView)findViewById(R.id.timeView)).setText(timeFormat(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mediaPlayer.seekTo(seekBar.getProgress()*1000);
                music_time = seekBar.getProgress();
            }
        });

        final Switch speed = (Switch) findViewById(R.id.speed);
        speed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlaybackParams params = mediaPlayer.getPlaybackParams();
                if (speed.isChecked()){
                    params.setSpeed(2);
                }
                else{
                    params.setSpeed(1);
                }
                mediaPlayer.setPlaybackParams(params);
            }
        });
    }

    private void initMediaPlayer(){
        AssetManager assetManager = getAssets();
        AssetFileDescriptor fd = null;
        try {
            fd = assetManager.openFd("music.mp3");
            mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            mediaPlayer.prepare();
            int time = mediaPlayer.getDuration()/1000;
            ((SeekBar)findViewById(R.id.seekBar)).setMax(time);
            ((TextView)findViewById(R.id.timeView)).setText("0:0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.release();
    }

    private String timeFormat(int time){
        return String.valueOf(time/60)+':'+String.valueOf(time%60);
    }
}